package com.epoint.androidphone.mobileoa.ui.webinfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.epoint.androidmobile.bizlogic.common.FileDownService;
import com.epoint.androidmobile.bizlogic.mail.model.SendAttachModel;
import com.epoint.androidmobile.bizlogic.webinfo.model.AttachModel;
import com.epoint.androidmobile.bizlogic.webinfo.model.WebInfoFeedBackInfoModel;
import com.epoint.androidmobile.bizlogic.webinfo.task.WebInfo_AddFeedBack_Task;
import com.epoint.androidmobile.bizlogic.webinfo.task.WebInfo_GetFeedBackView_Task;
import com.epoint.androidmobile.core.control.AlertUtil;
import com.epoint.androidmobile.core.control.ToastUtil;
import com.epoint.androidmobile.core.string.StringHelp;
import com.epoint.androidmobile.core.xml.XMLUtil;
import com.epoint.androidphone.R;
import com.epoint.androidphone.mobileoa.ui.init.InitView;
import com.epoint.androidphone.mobileoa.ui.superview.SuperPhonePage;

/**
 * @author Tracy
 * @time 2013-7-17 9:02:53
 * @annomation 信息简报反馈列表
 */
public class WebInfoFeedBackListView extends SuperPhonePage {

	long getWebInfoFeedBackListTaskId, addWebInfoFeedBackTaskId;

	ListView lv;
	List<WebInfoFeedBackInfoModel> inboxlist = new ArrayList<WebInfoFeedBackInfoModel>();
	LVAdapter adapter = new LVAdapter();
	String InfoGuid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addContentViewAndTitle(R.layout.mail_feedback_list, "信息反馈");
		lv = (ListView) findViewById(R.id.lv);
		lv.setOnItemClickListener(this);

		getBtRight().setText("反馈");
		getBtRight().setOnClickListener(this);
		getBtRight().setVisibility(View.VISIBLE);

		InfoGuid = getIntent().getStringExtra("InfoGuid");
		lv.setAdapter(adapter);
		getData();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO 自动生成的方法存根
		super.onItemClick(parent, view, position, id);
		WebInfoFeedBackInfoModel info = inboxlist.get(position);
		final ArrayList<AttachModel> attList = XMLUtil.DomAnalysisCommon(info.AttachFiles, AttachModel.class);
		if (attList.size() == 0) {
			return;
		}
		String[] attstr = new String[attList.size()];
		for (int i = 0; i < attList.size(); i++) {
			attstr[i] = attList.get(i).AttFileName;
		}
		AlertUtil.showAlertMenu(this, "附件下载", attstr,
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						AttachModel m = attList.get(which);
						FileDownService.download(getContext(),
								getString(R.string.WebServiceType),
								m.DownLoadUrl, m.isBigFile, InfoGuid,
								m.AttFileName, InitView.attachdownMainPath);
					}
				});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		if (v == getBtRight()) {
			View view = getInflater().inflate(R.layout.webinfo_add_feedback,
					null);
			final EditText et = (EditText) view
					.findViewById(R.id.webinfo_add_feedback_content);

			AlertDialog alertDialog = new AlertDialog.Builder(this)
					.setTitle("处理意见添加").setView(view)
					.setNegativeButton("添加", new OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							String FeedBackContent = et.getText().toString();
							if (null == FeedBackContent
									|| "".equals(FeedBackContent.trim())) {
								ToastUtil.toastWorning(
										WebInfoFeedBackListView.this,
										"反馈信息内容不能为空");
								return;
							}
							HashMap<String, Object> params = getPassMap();
							params.put("InfoGuid", InfoGuid);
							params.put("FeedBackContent", FeedBackContent);
							params.put("ATTACHList", new ArrayList<SendAttachModel>());
							addWebInfoFeedBackTaskId = new WebInfo_AddFeedBack_Task(
									WebInfoFeedBackListView.this, params)
									.startTask();

						}
					}).setPositiveButton("取消", new OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							// TODO Auto-generated method stub
							arg0.dismiss();
						}
					}).create();
			alertDialog.show();
		}
	}

	private void getData() {
		showTopProgressBar();
		HashMap<String, Object> param = getPassMap();
		param.put("InfoGuid", InfoGuid);
		getWebInfoFeedBackListTaskId = new WebInfo_GetFeedBackView_Task(this,
				param).startTask();
	}

	@Override
	public void refreshUI(long taskId, Object obj) {
		super.refreshUI(taskId, obj);
		if (taskId == getWebInfoFeedBackListTaskId) {
			lv.setVisibility(View.VISIBLE);
			if (validateXML(obj)) {
				String WebInfoListXml = StringHelp.getXMLAttOut(obj.toString(),
						"FeedBackList");
				inboxlist.clear();
				inboxlist = XMLUtil.DomAnalysisCommon(WebInfoListXml,
						WebInfoFeedBackInfoModel.class);

				adapter.notifyDataSetChanged();
			}
		} else if (taskId == addWebInfoFeedBackTaskId) {
			if (validateXML(obj)) {
				ToastUtil.toastShort(this, "添加成功");
				getData();
			}
		}
	}

	class LVAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO 自动生成的方法存根
			return inboxlist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO 自动生成的方法存根
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO 自动生成的方法存根
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			LayoutInflater mInflater = LayoutInflater.from(getContext());
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(
						R.layout.webinfo_feedback_list_item, null);
				holder.ivFj = (ImageButton) convertView
						.findViewById(R.id.iv_fj);
				holder.tvContent = (TextView) convertView
						.findViewById(R.id.tvContent);
				holder.tvSender = (TextView) convertView
						.findViewById(R.id.tvSender);
				holder.tvTime = (TextView) convertView
						.findViewById(R.id.tvTime);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			WebInfoFeedBackInfoModel a = inboxlist.get(position);

			holder.tvContent.setText(Html.fromHtml(a.FeedBackContent));
			holder.tvSender.setText(a.DispName + " 说：");
			holder.tvTime.setText(a.BackTime);
			if (a.AttachFiles.indexOf("AttachGuid") != -1) {
				holder.ivFj.setVisibility(View.VISIBLE);
			}

			return convertView;
		}

	}

	class ViewHolder {
		public ImageButton ivFj;
		public TextView tvContent;
		public TextView tvSender;
		public TextView tvTime;
	}
}
